import {AnDaCommitApi, UserCommitApi} from "@/api/user_common";

/**
 * 安达接口扩展类
 */
const ApiSystemScheduleUtil = {
  /**
   * 获取医生信息
   * @param self
   * @param apiData
   * @returns {Promise<void>}
   */
  async getScheduleListByTerminal(self, apiData) {
    let nooList = await this.getNoonList(self);
    // 全部午别信息
    if (nooList.length === 0) {
      return [];
    }
    // 获取排班数据
    const scheduleList = await this.getScheduleList(self);

    // 处理排班数据
    let resultList = this.resetScheduleList(self, scheduleList, nooList);
    return resultList;
  },

  /**
   * 处理排班数据
   * @param scheduleList
   * @param nooList
   * @param self
   * @param resultList
   * @returns {*}
   */
  resetScheduleList(self, scheduleList, nooList) {
    let resultList = [];
    try {
      const workerGroupArr = scheduleList.reduce((result, item) => {
        const key = item["workerId"];
        if (!result[key]) {
          result[key] = [];
        }
        result[key].push(item);
        return result;
      }, {});

      for (let workerId in workerGroupArr) {
        const workerScheduleList = [];
        // 初始化午别数据
        nooList.forEach((item) => {
          let tempRow = {
            noonId: item.noonId + "",
            startTime: item.startTime,
            noonName: item.noonName,
          };
          self.weekDays.forEach((weekKey, index) => {
            tempRow[weekKey] = {
              status: false,
              week: index
            };
          });
          workerScheduleList.push(tempRow);
        });
        // 开始渲染数据
        const tempList = workerGroupArr[workerId];
        // 开始组装数据
        tempList.forEach((item, index) => {
          // 获取星期机key
          let weekKey = self.weekDays[parseInt(item.week)];
          let tempNoonIds = item.noonIds ? item.noonIds.split(",") : [];
          for (let workerScheduleListElement of workerScheduleList) {
            // 变更状态
            if (tempNoonIds.includes(workerScheduleListElement.noonId + "")) {
              workerScheduleListElement[weekKey].status = true;
            }
          }
        });
        resultList.push({
          workerInfo: workerGroupArr[workerId][0].workerDetailInfo,
          orderNum: workerGroupArr[workerId][0].orderNum,
          scheduleList: workerScheduleList
        });
        // 排序
        resultList = resultList.sort(function (a, b) {
          return a.orderNum = b.orderNum;
        });
      }
    } catch (e) {
      console.log("排班数据解析错误");
    }
    return resultList;
  },

  /**
   * 获取午别
   * @param self
   * @returns {Promise<*[]>}
   */
  async getNoonList(self) {
    let noonList = [];
    // 获取接口数据
    const formJson = {
      tenantId: self.deviceInfo.hospitalId
    };
    const {data, code} = await UserCommitApi.getNoonList(formJson);
    if (200 === code) {
      noonList = data || [];
    }
    return noonList;
  },
  /**
   * 获取排班原始数据
   * @param self
   * @param apiData
   * @param callBack
   * @param backstageRefresh
   * @returns {Promise<*|*[]>}
   */
  async getScheduleList(self) {
    const formJson = {
      terminalId: self.deviceInfo.screenId,
      terminalType: self.deviceInfo.screenType,
      tenantId: self.deviceInfo.hospitalId
    };
    const {code, data} = await UserCommitApi.getScheduleListByTerminal(formJson);
    return data || [];
  }
};
export {ApiSystemScheduleUtil};
